package com.shiro.demo.Dao;

import com.shiro.demo.vo.User;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
@Component
public class UserdaoImol implements UserDao {
    @Resource
    JdbcTemplate jdbcTemplate;
    @Override
    public User getPasswordByuserName(String userName) {
        String sql = "select username, password from users where username= ?";
        List<User> list = jdbcTemplate.query(sql, new String[]{userName}, new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet resultSet, int i) throws SQLException {
                User user = new User();
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                return user;
            }
        });
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }



    public List<String> queryRolesBYuserName(String userName) {
        String sql="select role_name   from user_roles  where username= ?";
        return jdbcTemplate.query(sql,new String []{userName}, new RowMapper<String>() {
            @Override
            public String mapRow(ResultSet resultSet, int i) throws SQLException {
                return resultSet.getString("role_name");
            }
        });
    }
    public List<String> queryPermsByName(String userName) {
        // TODO Auto-generated method stub
        String sql = "select permission from roles_permissions where role_name = ?";
        return jdbcTemplate.queryForList(sql, new String[] {userName}, String.class);
    }
}
